package com.exposit.zbs.controller;

import com.exposit.zbs.model.GrantArea;
import com.exposit.zbs.service.IGrantApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;


/**
 * Created by Andrey on 14.08.2014.
 */

@Controller
@RequestMapping("/area")
public class AreaController {

	@Autowired
	IGrantApplicationService grantApplicationService;

	/**
	 * This method is used to display all grant application in grant area.
	 *
	 * @param grantAreaLink
	 * @param grantArea
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/{grantAreaLink}", method = RequestMethod.GET)
	public ModelAndView showGrantsInArea(@PathVariable String grantAreaLink, @PathVariable("grantAreaLink") GrantArea grantArea, HttpSession session) {

		ModelAndView mav = new ModelAndView("area.viewArea");
		mav.addObject("grantArea", grantArea);
		mav.addObject("grantApplications", grantApplicationService.getGrantsByArea(grantArea, 1500));
		BigDecimal availMoney = grantApplicationService.getAvailMoneyForArea(grantArea.getGrantAreaId());
		int moneySum = grantApplicationService.getAllMoney(grantArea.getGrantAreaId());
		mav.addObject("moneySum", moneySum);
		mav.addObject("moneyAvalable", availMoney);
		return mav;
	}


}
